﻿$(function() {
	var intervalCounter = 0;
	var app = angular.module('interfaceHeaderApp', [ 'toaster', 'ngAnimate' ]);
	app
			.controller(
					'interfaceHeaderCtrl',
					function($scope, $http, toaster) {
						// 解除绑定
						$scope.unbind = function() {
							$http(
									{
										method : 'POST',
										url : "temp/unbindTemp.do",
										headers : {
											'Content-Type' : 'application/x-www-form-urlencoded'
										},
										params : {}, // 传递数据作为字符串，从前台传到后台
									}).success(function(data) {// 这里的data，就是后台传递过来的数据
								if (data == 'true') {
									toaster.pop({
										type : 'success',
										title : '提示',
										body : "解绑成功",
										timeout : 2000
									});
									$scope.currentUseTempid = null;
								} else {
									toaster.pop({
										type : 'error',
										title : '提示',
										body : "解绑失败",
										timeout : 2000
									});
								}
							}).error(function(data, status, headers, config) {
								alert("请求出错了");
							});
						}
						// 使用lbt模板
						$scope.useTemp_lbt = function(x, y) {
							if (y == '解绑') {
								$scope.unbind();
								$scope.currentUseTempid = null;
							} else {
								$http(
										{
											method : 'POST',
											url : "temp/useTemp_lbt.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {
												tLbttempid : x.tLbttempid,
												tWqttempid : x.t_wqttempid,
												tZztttempid : x.t_zztttempid,
												projectname : $scope.id
											}, // 传递数据作为字符串，从前台传到后台
										})
										.success(
												function(data) {// 这里的data，就是后台传递过来的数据
													if (data == 'true') {
														toaster.pop({
															type : 'success',
															title : '提示',
															body : "绑定成功",
															timeout : 2000
														});
														if ($scope.id == 'wqtweb') {
															$scope.currentUseTempid = x.t_wqttempid;
														} else if ($scope.id == 'lbt') {
															$scope.currentUseTempid = x.tLbttempid;
														} else if ($scope.id == 'zzttweb') {
															$scope.currentUseTempid = x.t_zztttempid;
														} else {
															$scope.currentUseTempid = null;
														}

													} else {
														toaster.pop({
															type : 'error',
															title : '提示',
															body : "使用失败",
															timeout : 2000
														});
													}
												}).error(
												function(data, status, headers,
														config) {
													alert("请求出错了");
												});
							}
						}
						// 删除单个个人模板
						$scope.deleteTemp = function(x) {
							$http(
									{
										method : 'POST',
										url : "temp/deleteTemp.do",
										headers : {
											'Content-Type' : 'application/x-www-form-urlencoded'
										},
										params : {
											tLbttempid : x.tLbttempid,
											tWqttempid : x.t_wqttempid,
											tZztttempid : x.t_zztttempid,
											projectname : $scope.id
										}, // 传递数据作为字符串，从前台传到后台
									}).success(
									function(data) {// 这里的data，就是后台传递过来的数据
										if (data == 'true') {
											$scope.names.splice($scope.names
													.indexOf(x), 1);
											toaster.pop({
												type : 'success',
												title : '提示',
												body : "已删除",
												timeout : 2000
											});
										} else {
											toaster.pop({
												type : 'error',
												title : '提示',
												body : "删除失败",
												timeout : 2000
											});
										}
									}).error(
									function(data, status, headers, config) {
										alert("请求出错了");
									});
						}
						// 鼠标点击事件
						$scope.show = function(x) {
							/* toaster.pop('success', "提示", x); */
							toaster.pop({
								type : 'success',
								title : '提示',
								body : x,
								timeout : 1000
							});
						}

						// 显示为使用按钮还是解绑按钮
						$scope.isUseBtn = function(x) {
							if (x.tLbttempid != null
									&& x.tLbttempid == $scope.currentUseTempid) {
								return "解绑";
							}
							if (x.t_wqttempid != null
									&& x.t_wqttempid == $scope.currentUseTempid) {
								return "解绑";
							}
							if (x.t_zztttempid != null
									&& x.t_zztttempid == $scope.currentUseTempid) {
								return "解绑";
							}
							return "绑定";
						}
						// 配置多个tr，当为lbt的时候就显示出来，其它项目可以灵活用
						$scope.lbtShow = false;
						// 配置首页查询的标头，默认为不显示
						$scope.showTh = false;
						// 配置首页查询的标头，及对应的查询请求
						$scope.thChange = function() {
							$scope.names = null;
							if ($scope.id == 'lbt') {
								$http(
										{
											method : 'GET',
											url : "temp/findLbtTemp.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {}, // 传递数据作为字符串，从前台传到后台
										})
										.success(
												function(data) {// 这里的data，就是后台传递过来的数据
													if (data.tag == 'success') {
														if (data.t_use != null
																&& data.t_use.projectname == $scope.id) {
															$scope.currentUseTempid = data.t_use.tempid;
														} else {
															$scope.currentUseTempid = null;
														}
														$scope.names = data.result;
													} else {
														toaster.pop({
															type : 'error',
															title : '提示',
															body : "无查询结果",
															timeout : 2000
														});
													}
												}).error(
												function(data, status, headers,
														config) {
													alert("请求出错了");
												});
								$scope.showTh = true;
							} else if ($scope.id == 'wqtweb') {
								$http(
										{
											method : 'GET',
											url : "temp/findWqtTemp.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {}, // 传递数据作为字符串，从前台传到后台
										})
										.success(
												function(data) {// 这里的data，就是后台传递过来的数据
													if (data.tag == 'success') {
														if (data.t_use != null
																&& data.t_use.projectname == $scope.id) {
															$scope.currentUseTempid = data.t_use.tempid;
														} else {
															$scope.currentUseTempid = null;
														}
														$scope.names = data.result;
													} else {
														$scope
																.toasterError("无查询结果");
													}
												}).error(
												function(data, status, headers,
														config) {
													alert("请求出错了");
												});
								$scope.showWqtWeb = true;
							} else if ($scope.id == 'zzttweb') {
								$http(
										{
											method : 'GET',
											url : "temp/findzzttTemp.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {}, // 传递数据作为字符串，从前台传到后台
										})
										.success(
												function(data) {// 这里的data，就是后台传递过来的数据
													if (data.tag == 'success') {
														if (data.t_use != null
																&& data.t_use.projectname == $scope.id) {
															$scope.currentUseTempid = data.t_use.tempid;
														} else {
															$scope.currentUseTempid = null;
														}
														$scope.names = data.result;
													} else {
														$scope
																.toasterError("无查询结果");
													}
												}).error(
												function(data, status, headers,
														config) {
													alert("请求出错了");
												});
								$scope.showzzttWeb = true;
							} else {
								$scope.showTh = false;
								$scope.showWqtWeb = false;
								$scope.showzzttWeb = false;
							}
						}
						// 显示配置模板页面
						$scope.addtemp = function() {
							$("#spm").show();
							var target = $(".Popup");
							var targetWidth = target.outerWidth();
							var targetHeight = target.outerHeight();
							target.css({
								"margin-top" : -parseInt(targetHeight / 2)
										+ "px",
								"margin-left" : -parseInt(targetWidth / 2)
										+ "px"
							});
							target.show();
						}
						// 为lbt,wqtweb配置的添加功能的标头
						$scope.changePro_tem = function() {
							if ($scope.project_tem == 'lbt') {
								$scope.lbtShow = true;
							} else if ($scope.project_tem == 'wqtweb') {
								$scope.wqtWebShow = true;
							} else if ($scope.project_tem == 'zzttweb') {
								$scope.zzttWebShow = true;
							} else {
								$scope.lbtShow = false;
								$scope.wqtWebShow = false;
								$scope.zzttWebShow = false;
							}
						}
						// 关闭弹出框
						$scope.pupuClose = function() {
							$("#spm").hide();
							$(".Popup").hide();
						}
						// 提交保存个人header配置
						$scope.saveTemp = function() {
							if ($scope.project_tem == 'lbt') {
								if ($scope.url == undefined
										|| $scope.url.trim() == ""
										|| $scope.loginName == undefined
										|| $scope.loginName.trim() == ""
										|| $scope.passWord == undefined
										|| $scope.passWord.trim() == "") {
									toasterError("url,loginname,password为必填项");
								} else {
									$scope.loginName = $scope.loginName == undefined ? ""
											: $scope.loginName;
									$scope.platform = $scope.platform == undefined ? ""
											: $scope.platform;
									$scope.sysver = $scope.sysver == undefined ? ""
											: $scope.sysver;
									$scope.phonemodel = $scope.phonemodel == undefined ? ""
											: $scope.phonemodel;
									$scope.passWord = $scope.passWord == undefined ? ""
											: $scope.passWord;
									$scope.appver = $scope.appver == undefined ? ""
											: $scope.appver;
									$scope.imsi = $scope.imsi == undefined ? ""
											: $scope.imsi;
									$http(
											{
												method : 'post',
												url : "temp/saveTemp.do",
												headers : {
													'Content-Type' : 'application/x-www-form-urlencoded'
												},
												params : {
													tempName : "lbt",
													url : encodeURI($scope.url),
													loginName : encodeURI($scope.loginName),
													platform : encodeURI($scope.platform),
													sysver : encodeURI($scope.sysver),
													phonemodel : encodeURI($scope.phonemodel),
													passWord : encodeURI($scope.passWord),
													appver : encodeURI($scope.appver),
													imsi : encodeURI($scope.imsi)
												}, // 传递数据作为字符串，从前台传到后台
											}).success(function(data) {// 这里的data，就是后台传递过来的数据
										if (data == 'true') {
											$("#spm").hide();
											$(".Popup").hide();
											toaster.pop({
												type : 'success',
												title : '提示',
												body : "添加成功",
												timeout : 2000
											});
										} else {
											toaster.pop({
												type : 'error',
												title : '提示',
												body : "添加失败",
												timeout : 2000
											});
										}
									}).error(
											function(data, status, headers,
													config) {
												alert("请求出错了");
											});
								}
							} else if ($scope.project_tem == 'wqtweb') {
								if ($scope.userName == undefined
										|| $scope.userName.trim() == ""
										|| $scope.userPassword == undefined
										|| $scope.userPassword.trim() == ""
										|| $scope.wqtweburl == undefined
										|| $scope.wqtweburl.trim() == "") {
									$scope
											.toasterError("wqtweburl,userName,userPassword为必填项");
								} else {
									$scope.v_qy_Num = $scope.v_qy_Num == undefined ? ""
											: $scope.v_qy_Num;
									$http(
											{
												method : 'post',
												url : "temp/saveTemp.do",
												headers : {
													'Content-Type' : 'application/x-www-form-urlencoded'
												},
												params : {
													tempName : "wqtweb",
													wqtweburl : encodeURI($scope.wqtweburl),
													userName : encodeURI($scope.userName),
													userPassword : encodeURI($scope.userPassword),
													v_qy_Num : encodeURI($scope.v_qy_Num)
												}, // 传递数据作为字符串，从前台传到后台
											}).success(function(data) {// 这里的data，就是后台传递过来的数据
										if (data == 'true') {
											$("#spm").hide();
											$(".Popup").hide();
											$scope.toasterSuccess("添加成功");
										} else {
											$scope.toasterError("添加失败");
										}
									}).error(
											function(data, status, headers,
													config) {
												$scope.toasterError("请求出错");
											});
								}
							} else if ($scope.project_tem == 'zzttweb') {
								if ($scope.zzttuserName == undefined
										|| $scope.zzttuserName.trim() == ""
										|| $scope.zzttuserPassword == undefined
										|| $scope.zzttuserPassword.trim() == ""
										|| $scope.zzttweburl == undefined
										|| $scope.zzttweburl.trim() == ""
										|| $scope.zzttenamesubcomp == undefined
										|| $scope.zzttenamesubcomp.trim() == "") {
									$scope
											.toasterError("url,userName,Password,subcomp,为必填项");
								} else {
									$scope.v_qy_Num = $scope.v_qy_Num == undefined ? ""
											: $scope.v_qy_Num;
									$http(
											{
												method : 'post',
												url : "temp/saveTemp.do",
												headers : {
													'Content-Type' : 'application/x-www-form-urlencoded'
												},
												params : {
													tempName : "zzttweb",
													zzttweburl : encodeURI($scope.zzttweburl),
													zzttuserName : encodeURI($scope.zzttuserName),
													zzttuserPassword : encodeURI($scope.zzttuserPassword),
													zzttenamesubcomp : encodeURI($scope.zzttenamesubcomp)
												}, // 传递数据作为字符串，从前台传到后台
											}).success(function(data) {// 这里的data，就是后台传递过来的数据
										if (data == 'true') {
											$("#spm").hide();
											$(".Popup").hide();
											$scope.toasterSuccess("添加成功");
										} else {
											$scope.toasterError("添加失败");
										}
									}).error(
											function(data, status, headers,
													config) {
												$scope.toasterError("请求出错");
											});
								}
							} else {
							}

						}
						$scope.toasterError = function(x) {
							toaster.pop({
								type : 'error',
								title : '提示',
								body : x,
								timeout : 2000
							});
						}
						$scope.toasterSuccess = function(x) {
							toaster.pop({
								type : 'success',
								title : '提示',
								body : x,
								timeout : 2000
							});
						}
					});
})
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
